####S Big function
WriteNRandomBibItems<-function(data.to.load,data.to.save,n.,files.to.remove){
#' Randomly select a subset of bibliography items
#' @param data.to.load (character) full path and name of input .bib file (including extension)
#' @param data.to.save (character) full path and name of output .bib file (including extension)
#' @param n. (integer) number of random samples that you want
#' @param files.to.remov (data.frame) results from the EvaluatedCheck function.
#' @author Joseph Henry \email{joseph.henry@@sydney.edu.au}
#' @date 25 May 2012 (last modified 18 December 2012)

#' Packages
require(stringr)

#' Little functions
to_str_func<-function(input.){
#' Make into one big string
o1 <- gsub (",","#66666666#",input.) # Need to put the hash in or else dates and other numbers that contain a six and were followed by a comma are picked, leave 6 in the string
o2 <- toString (o1)
o3 <- gsub (","," ",o2)
o4 <- gsub("#66666666#",",",o3)
return(o4)
}
remove_tab_func<-function(input.){
#' Remove tab delimiter
o1<-gsub("\t","",input.)
return(o1)
}
to_line_func<-function(input.){
#' Split the string into lines for each reference
o1<-unlist(strsplit(input.,"} },"))
o2<-c()
for(i in 1:(length(o1)-1)){
o2[i]<-paste(o1[i],"} },")
}
return(o2)
}
bibtex_key_gen_func<-function(input.){
#' Part of bibtex_key_func_final
# Check which features are in the bib item
au.test<-str_detect(input.,"author = \\{")
da.test<-str_detect(input.,"year = \\{")
pa.test<-str_detect(input.,"pages = \\{")
# Extract data
if (au.test == TRUE){
au.fin<- gsub ("author = \\{","",str_extract (input.,"author = \\{[A-Za-z]*"))
} else {
au.fin<-"_"
}
if (da.test == TRUE){
da.fin<- substring (gsub ("year = \\{","",str_extract (input.,"year = \\{[0-9]{4}")),3,4)
} else {
da.fin <- "_"
}
if (pa.test == TRUE){
pa.fin <- gsub ("pages = \\{","",str_extract (input.,"pages = \\{[0-9]{4}"))
} else {
pa.fin <- "_"
}
o1 <- paste (da.fin,au.fin,pa.fin,sep="_")
return(o1)
}
bibtex_key_wri_func<-function(input.A,input.B){
#' Part of bibtex_key_func_final
o1 <- str_replace (input.A, "(\\{)(.*?)(,)","\\199999999\\3")
o1 <- str_replace (input.A, "(\\{)(.*?)(,)","\\199999999\\3") # Remove keywords
o2<-c()
for(i in 1 : length (o1)){
o2[i] <- gsub ("99999999",input.B[i],o1[i])
}
return(o2)
}
bibtex_key_func_final<-function(input.){
#' Generate a bibtex key that is date_first author_maxpages. There could be more work done on this, i.e. page numbers, but it is probably unecessary.
o1 <- as.character (sapply (input.,function(x)bibtex_key_gen_func(x))) # Generate bibkey
o2 <- bibtex_key_wri_func (input.,o1) # Overwrite old one
return (o2)
}
bibtex_remove_unmarked<-function(input.a,input.b){
#' Get rid of files not marked off by reviewer 1
EC2 <- as.character(input.a[input.a[,2]!="done",1]) # find non-marked bibs
EC3 <- gsub("\t| ","",EC2)
lines_to_remove <- sapply(EC3,function(x){
o1 <- grep(x,input.b)
})
o2 <- input.b[-unlist(lines_to_remove)]
return(o2)
}

#' Big function
#' Load
load_data <- readLines (data.to.load)
#' Clean
clean_data_1 <- to_str_func (load_data)
clean_data_2 <- remove_tab_func (clean_data_1)
clean_data_3 <- to_line_func (clean_data_2)
clean_data_4 <- bibtex_key_func_final (clean_data_3)

# Remove keyword data
#' Do
Split.into.lines.raw <- paste("@",strsplit(clean_data_4,"@")[[1]],sep="")[-1]
if(length(Split.into.lines.raw)==1&length(clean_data_4)!=1){
Split.into.lines.raw <- sapply(strsplit(clean_data_4,"@"),function(x)paste("@",x[2],sep=""))
}
Split.into.lines<-bibtex_remove_unmarked(files.to.remove,Split.into.lines.raw)
clean_data_5 <- paste (as.character ( sapply (Split.into.lines,
function (x) {
o1 <- str_replace (x, "(keywords)(.*?)(=)(.*?)(\\{)(.*)(\\})","\\1\\3\\5\\7\\8")
return (o1)
})), "}")
Potential_items <- length (clean_data_5)
n._check <-ifelse (n. <= Potential_items,"CONTINUE","STOP")
if(n._check == "CONTINUE"){
random.numbers <- sample (1 : Potential_items, n.) # Select random numbers
do_data_1 <- clean_data_5[random.numbers] # Grab those files
if(file.exists (data.to.save) == FALSE){ # Write if file doesn't already exist
writeLines(do_data_1,data.to.save)
}
}
if(n._check == "STOP"){
print(paste("There are more numbers to select than items in the bibliography. 'n.' must be less than ",length(Split.into.lines) - 1,"."))
}
}

###S Run function
	 WriteNRandomBibItems(data.to.load,data.to.save,n.,files.to.remove)
###E